<template>
  <div>
    <el-dialog v-bind="$attrs" :title="$attrs.title" v-on="$listeners" @open="onOpen" @close="onClose">
      <el-form ref="formRef" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入权限名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" placeholder="请输入权限标识" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" placeholder="请输入权限描述" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="开启" prop="enVisible" required>
          <el-switch v-model="formData.enVisible" active-value="1" inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { addPermission, updatePermission } from '@/api/permisson'
export default {
  components: {},
  inheritAttrs: false,
  props: [],
  data() {
    return {
      formData: {
        name: undefined,
        code: undefined,
        description: undefined,
        enVisible: false,
        type: undefined,
        pid: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入权限名称',
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入权限标识',
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: '请输入权限描述',
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    console.log(this.$attrs)
    console.log(this.$listeners)
  },
  methods: {
    onOpen() {},
    onClose() {
      this.$refs['formRef'].resetFields()
    },
    close() {
      // this.$emit('update:visible', false)
      this.$emit('close') // 关闭对话框
    },
    handelConfirm() {
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        if (this.formData.id) {
          // 编辑
          await updatePermission(this.formData)
        } else {
          // 添加
          await addPermission(this.formData) // 发请求
        }
        this.$message.success('操作成功')
        this.$parent.getPermissionList()// 刷新数据
        this.close()
      })
    }
  }
}

</script>
<style>
</style>
